import { useRequest } from 'umi';
import { useMemo, useState } from 'react';
import { MaterialManageTab } from './TabSwitch';
import { getCompanyID, getPosIDs, getTenantID, getUserDepID, getUserID, requestFunComponFiex } from '@/utils';
import { ILoadMapTypeAll } from '@/common';
import { getBaseUrl } from '@/services/urlconst';

const baseUrl = getBaseUrl();

const disposeArea = (resObj: Record<string, any>): any => {
  /***处理省市县**/
  if (resObj?.contaNode?.length) {
    let CodeArr = [...resObj?.contaNode];
    resObj.provinceCode = CodeArr?.[0];
    resObj.cityCode = CodeArr?.[1];
    resObj.countyCode = CodeArr?.[2];
  }
  return resObj;
};
export const MaterialService = () => {
  // 物料列表接口
  const getMaterialService = ({ curRes }: Record<string, any>) => {
    /***保存当前的搜索参数**/
    const [manageTableParams, setManageTableParams] = useState<Record<string, any>>({});

    const request = useRequest(
      (requestParams?: Record<string, any>) => {
        let bodyParams: any = {
          current: 1,
          pageSize: 10,
          state: '2',
          supplyType: `${getTenantID()}${ILoadMapTypeAll.PURCHASE_CATEGORY}1000`,
          ...requestParams,
          ...MaterialManageTab(curRes),
        };

        setManageTableParams(bodyParams);
        const reqUrl = `${baseUrl}/materialController/dataGrid/${bodyParams?.params}`;
        return requestFunComponFiex('POST', reqUrl, bodyParams);
      },
      {
        manual: false,
        paginated: true,
        defaultPageSize: 10,
      },
    );
    const dataSource = useMemo(() => {
      if (request.data?.rows && request.data?.rows?.length) {
        return request.data?.rows?.map((item) => {
          return {
            ...item,
            needParams: item?.specInfo, // needParams[需求物料表中]  specInfo[物料表中]   字段替换
          };
        });
      } else {
        return [];
      }
    }, [request.data?.rows]);
    return {
      getMaterialRun: (params: any) => {
        request.run(params);
      },
      getMaterialDataSource: dataSource,
      getMaterialLoading: request.loading,
      getMaterialTableProps: request.tableProps,
      manageTableParams,
    };
  };

  // 物料详情接口
  const findMaterialService = () => {
    const request: Record<string, any> = useRequest(
      (payload: Record<string, any>) => {
        if (!payload.id) return;
        const reqUrl = `${baseUrl}/materialController/details/${payload.id}`;
        return requestFunComponFiex('GET', reqUrl, {});
      },
      {
        manual: false,
        paginated: true,
        defaultPageSize: 10,
      },
    );
    return {
      findMaterialRun: (params: any) => request.run(params),
      findMaterialLoading: request.loading,
    };
  };

  // 申请物料接口
  const addMaterialService = (Callback: (res: Record<string, any>) => void) => {
    const request = useRequest(
      (payload) => {
        let bodyParams = {
          ...payload,
          signType: 1,
          createrID: getUserID(),
          createPositionID: getPosIDs(),
          createDepartmentID: getUserDepID(),
          createCompanyID: getCompanyID(),
        };
        bodyParams = disposeArea(bodyParams);
        let reqUrl = `${baseUrl}/materialController/applyMaterial`;
        return requestFunComponFiex('POST', reqUrl, bodyParams);
      },
      {
        manual: true,
        onSuccess: Callback,
      },
    );

    return {
      addMaterialRun: request?.run,
      addMaterialLoading: request?.loading,
    };
  };

  return {
    getMaterialService,
    findMaterialService,
    addMaterialService,
  };
};
